home *** CD-ROM | disk | FTP | other *** search
- Path: lrz-muenchen.de!news
- From: watzka@stat.uni-muenchen.de (Kurt Watzka)
- Newsgroups: comp.lang.c
- Subject: Re: Q: How to assign structures?
- Date: 9 Mar 1996 19:45:52 GMT
- Organization: Leibniz-Rechenzentrum, Muenchen (Germany)
- Distribution: world
- Message-ID: <4hsn5g$44s@sparcserver.lrz-muenchen.de>
- References: <4gsalu$dll@ccshst05.cs.uoguelph.ca> <4hse0f$efq@SNFC21_SRVR_WWW.PBI.net>
- NNTP-Posting-Host: sun2.lrz-muenchen.de
-
- mich@pbinet.com writes:
-
- >In <4gsalu$dll@ccshst05.cs.uoguelph.ca>, thay@uoguelph.ca (Toby K Hay) writes:
- >>I need to assign values to structures in one array from structures in
- >>another array - in essence I'm copying the whole structure from one array
- >>to the other. Can I use simple assignment like this:
-
- >>struct StructName {
- >> int IVal;
- >> float FVal;}
-
- >>struct StructName Array1[5], Array2[5];
- >>.. . .
- >>for (i=0;i<5;i++) Array1[i] = Array2[i];
- >>.. . .
- >>And if not that, can I use memcpy() and sizeof() to do the assignment:
-
- >>for (i=0;i<5;i++) memcpy(&Array2[i],&Array1[i],sizeof(struct StructName));
- >>Or do I have to assign each element of the structure?
-
- >It would be a lot easier to create a pointer to the struct and then make a copy
- >of the pointer;
-
- I beg to differ. Copying pointers is _not_ equivalent to copying values.
- Consider an algorithm that will change the contents of the array
- of structures. Since you want to _keep_ the original values as well,
- what would you do using your approach?
-
- It _may_ be useful to pass a pointer to a struct to a function instead
- of the whole struct, but sometimes you just _need_ the call by value
- semantics that C uses in all function calls.
-
- >struct mystruct {..} ms, *ps;
-
- >main()
- >{
- > void *ns;
-
- Why on earth do we need a void * in this program.
-
- > ps = &ms;
- > ns = ps;
- >}
-
- Kurt
- --
- | Kurt Watzka Phone : +49-89-2180-6254
- | watzka@stat.uni-muenchen.de
- | ua302aa@sunmail.lrz-muenchen.de
-